↜ Back to index Introduction to Numerical Analysis 2
Fortran cheatsheet
Reference
Introduction to Programming with Fortran: a textbook from Springer, can be downloaded for free from Kanazawa University network
Compiling
To run program, it must be compiled first. For example, save the following in helloworld.f:
print *,'Hello, world!'
endNote that each line has to have 6 empty spaces (空白) at the beginning. This requirement is from the distant past when Fortran programs were written on punch cards (パンチカード).
Then compile it and run it:
$ gfortran helloworld.f
$ ./a.exe
Hello, world!
Or on one line:
$ gfortran helloworld.f && ./a.exe
Hello, world!
Since we live in 21st century and do not use punch cards anymore, I prefer to not have to worry about the empty spaces at the beginning of each line. Simply change the file extension (ファイルの拡張子) to .f90 which enables Fortran 90 style.
helloworld.f90:
print *,'Hello, world!'
endNo need for empty spaces! Other things are the same. Feel free to use either style with the appropriate file extension.
Run it as
$ gfortran helloworld.f90 && ./a.exe
Hello, world!
What follows assumes that the file extension is .f90.
Comments
Anything that follows ! is assumed to be a comment and ignored:
! This is a comment.Variables
Variables with names starting with i, j, …, n are implicitly defined to be integers, everything else are real numbers.
We can (and should) be more explicit:
implicit none ! prevents implicitly declared variables
integer i, j, k ! integer
real x, z ! real number
real y(5) ! array of 5 real numbers
real, dimension(5) :: ym ! same type as y, but more "modern"
! It is convenient when defining more variables
! with the same dimension.
i = 1 ! an assignment
x = 2.
y = [ 1., 2., 3., 4., 5. ] ! an array literal
endNote that :: is required in the declaration if more than one keywords are before the names of the variables.
Constants
Constants can be declared similarly to variables, by adding the parameter keyword. The value of constants cannot be modified.
integer, parameter :: n = 5Exercise 1. What happens if you try to modify a constant in a code?
Arithmetic operators
**: power x^y*: multiplication/: division+: addition-: subtraction
Built-in functions
sqrt(x): square root \sqrt{x}abs(x): absolute value |x|cos(x),sin(x),tan(x): trigonometric functionsacos(x),asin(x),atan(x): inverse trigonometric functions
do loop
implicit none
integer :: i
real, dimension(5) :: y ! array of 5 real numbers
do i=1,5
y(i) = 2 * i ! perform this code for i taking values
! 1, 2, 3, 4, 5
enddo
do i=1,5
print *, y(i)
enddo
endThe loop variable (i in the above program) should be an integer. If you use real you will see a warning that this feature is deprecated, but the program will still run as intended.
You can use exit to stop the loop early:
implicit none
integer i
do i=1,5
print *, i
if (i >= 3) then
exit ! exit the do loop
endif
enddo
print *, "do loop finished"
endif condition
if (2 > 1) then ! 2 > 1 is a test
print *,'2 is bigger than 1!' ! executed if test is true
else
print *,'Math is wrong!' ! executed if test is false
endif
endThe else part can be omitted:
if (2 * 2 /= 4) then
print *,'Math is wrong!'
endif
endImportant. There should be no other commands on the line after then. This might lead to very confusing bugs. Stick to one command per line.
Here are some operators for comparing numbers:
==,.eq.equal/=,.ne.not equal<=,.le.less than or equal to>=,.ge.greater than or equal to<,.lt.less than>,.gt.greater than
We can also combine tests using logical .and., .or. and negate them by .not..
Example. (a < b) .or. (a > b) is the same as a /= b.
Stopping the program early
Use the stop command.
implicit none
real a
print *, 'Enter a nonzero number:'
read *, a
if (a == 0) then
print *,"a must not be zero!"
stop
endif
print *, 1/a
endExercise 2. Can you replace stop with end? Can you replace end with stop?
Modern program structure
The recommended program structure these days is:
program ProgramName
implicit none ! no implicit variables
! varible declarations
! program content
end program ProgramNameBreaking long lines
Long lines can be broken using & character at the end.
implicit none
integer i
! break a line using &
i = 5 &
+ 3 &
! comments are ignored
- 2
! prints 6 = 5 + 3 - 2
print *, i
! You can even break strings. Note that there is no space in `very`
print *, 'This is a ve&
ry long string'
end